﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.ServiceModel.Syndication
Imports CMISObjectModel.Constants
Namespace Syndication.ServiceDocument
   ''' <summary>
   ''' Container for the RepositoryInfo. This class is needed to convert the RepositoryInfo to a Atom document.
   ''' </summary>
   ''' <remarks></remarks>
   Public Class CmisWorkspace
      Inherits ServiceModel.Syndication.Workspace

      ''' <summary>
      ''' RepositoryInfo to use
      ''' </summary>
      ''' <remarks></remarks>
      Private _repositoryInfo As CMISObjectModel.ServiceDocument.RepositoryInfo

      Public Sub New(repositoryInfo As CMISObjectModel.ServiceDocument.RepositoryInfo, baseUri As Uri)
         _repositoryInfo = repositoryInfo

         'Set the Title of the atom document
         Me.Title = New TextSyndicationContent(repositoryInfo.RepositoryName)

         'Set the baseuri
         Me.BaseUri = baseUri

         'Convert the RepositoryInfo into this class.
         Me.AddCollections()
         Me.AddLinks()
         Me.AddRepositoryInfo()
         Me.AddUriTemplate()
      End Sub

#Region "SyndicationLinks"
      ''' <summary>
      ''' Converts a CMISLink to a xml XAttribute.
      ''' </summary>
      ''' <param name="link"></param>
      ''' <returns></returns>
      ''' <remarks></remarks>
      Private Function CreateLink(link As CMISLink) As XAttribute()

         Return New XAttribute() {
            New XAttribute("type", link.MediaType),
            New XAttribute("rel", link.Rel),
            New XAttribute("href", link.Uri.AbsoluteUri)}

      End Function
      ''' <summary>
      ''' Adds the atom links returned by the RepositoryInfo into this class.
      ''' </summary>
      ''' <remarks></remarks>
      Private Sub AddLinks()
         Dim atomns As XNamespace = CmisNamespaceConstants.Atom

         Dim links As List(Of CMISLink) = _repositoryInfo.GetAtomLinks(Me.BaseUri)
         For Each link As CMISLink In links
            Me.ElementExtensions.Add(New XElement(atomns + "link", CreateLink(link), [String].Empty))
         Next
      End Sub

#End Region

#Region "Atom Extensions"

      ''' <summary>
      ''' Adds the repository Info into this class.
      ''' </summary>
      ''' <remarks></remarks>
      Private Sub AddRepositoryInfo()
         Me.ElementExtensions.Add("repositoryInfo", CmisNamespaceConstants.Cmisra, _repositoryInfo)
      End Sub


      ''' <summary>
      ''' Adds the uri templates returned by the RepositoryInfo into this class.
      ''' </summary>
      ''' <remarks></remarks>
      Private Sub AddUriTemplate()
         For Each uriTemplate As CMISObjectModel.ServiceDocument.CMISUriTemplate In _repositoryInfo.GetUriTemplates(Me.BaseUri)
            Me.ElementExtensions.Add("uritemplate", CmisNamespaceConstants.Cmisra, uriTemplate)
         Next
      End Sub



#End Region

#Region "Collections"
      ''' <summary>
      ''' Adds the collections returned by the RepositoryInfo into this class.
      ''' </summary>
      ''' <remarks></remarks>
      Private Sub AddCollections()

         For Each collection As ResourceCollectionInfo In _repositoryInfo.GetCollections(Me.BaseUri)
            Me.Collections.Add(collection)
         Next
      End Sub

#End Region
   End Class
End Namespace